package org.ling.xuan.extention;

import com.intellij.execution.impl.ConsoleViewImpl;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.config.DbConfig;
import org.ling.xuan.util.FileLogUtil;
import org.ling.xuan.util.LoggerUtil;

import javax.swing.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class LxLog implements ToolWindowFactory {
    private static final Lock lock = new ReentrantLock();
    private ConsoleView consoleView;

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        lock.lock();
        try {
            FileLogUtil.log("初始化底部工具栏开始:{}",project.getBasePath());
            this.consoleView = new ConsoleViewImpl(project, false);
            JComponent component = consoleView.getComponent();
            ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
            Content content = contentFactory.createContent(component, "", false);
            toolWindow.getContentManager().addContent(content);
            LoggerUtil.add(consoleView, project);
            FileLogUtil.log("初始化底部工具栏成功!");
            LoggerUtil.log(project, "欢迎使用LxTool");
            LoggerUtil.log(project, "插件初始化中...");
            DbConfig.getJdbcTemplate();
            LoggerUtil.log(project, "插件初始化完成^-^");
        } finally {
            lock.unlock();
        }
    }

    public ConsoleView getConsoleView() {
        return consoleView;
    }
}

